package cn.flying.cloud.task.core.quartz.configuration;

import javax.annotation.Resource;

import org.quartz.ListenerManager;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import cn.flying.cloud.task.core.quartz.listener.QuartzJobListener;

/**
 * Quartz 配置类
 *
 * @author: admin
 * @date: 2024年01月15日 18:20
 * @version: 1.0
 */
@Configuration
public class QuartzConfiguration {

    @Resource
    private SchedulerFactoryBean schedulerFactoryBean;
    @Resource
    private QuartzJobListener quartzJobListener;

    /**
     * @return
     * @throws SchedulerException
     */
    @Primary
    @Bean
    public Scheduler scheduler() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        ListenerManager listenerManager = scheduler.getListenerManager();
        //为job添加监听器
        listenerManager.addJobListener(quartzJobListener);
        return scheduler;
    }
}
